package fr.labri.harmony.analysis.jsep.results;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
 * 
 * Should not be launched anymore
 * 
 */
public class ScoreProducer {
	public static void main(String[] args) throws SQLException, FileNotFoundException, SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException {

		Set<String> rules = new HashSet<>();
		Set<String> wrong_rules = new HashSet<>();
		try {
			List<String> lines = Files.readAllLines(Paths.get("Rules.csv"), Charset.forName("UTF-8"));
			for(String line : lines) {
				String tk[] = line.split("\\;");
				if(tk.length>=4 && tk[3]!=null){
					if(tk[3].equals("true")) {
						rules.add(tk[0]+":"+tk[1]);
						rules.add(tk[1]+":"+tk[0]);
					}
					else if(tk[3].equals("false")) {
						wrong_rules.add(tk[0]+":"+tk[1]);
						wrong_rules.add(tk[1]+":"+tk[0]);
					}
				}
			}
			for(Pair<String,String> pair : LoadRules.getAllTrueRules()) {
				rules.add(pair.getElement0()+":"+pair.getElement1());
				rules.add(pair.getElement1()+":"+pair.getElement0());
			}
			
			int oldSize = wrong_rules.size();
			for(String rule : rules) {
				String tk[] = rule.split("\\:");
				String src=tk[0], tgt=tk[1];
				Set<String> news = new HashSet<String>();
				for(String wrong : wrong_rules) {
					String wtk[] = wrong.split("\\:");
					String wsrc=wtk[0], wtgt=wtk[1];
					if(src.equals(wsrc)) {
						news.add(wtgt+":"+tgt); news.add(tgt+":"+wtgt);
					}
					if(src.equals(wtgt)) {
						news.add(wsrc+":"+tgt); news.add(tgt+":"+wsrc);
					}
					if(tgt.equals(wsrc)) {
						news.add(wtgt+":"+src); news.add(src+":"+wtgt);
					}
					if(tgt.equals(wtgt)) {
						news.add(src+":"+wsrc); news.add(src+":"+wsrc);
					}
				}
				wrong_rules.addAll(news);
			}
			System.out.println(wrong_rules.size()-oldSize+" wrong rules found");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Rules true "+rules.size());
		System.out.println("Rules wrong "+wrong_rules.size());
		PrintStream ps = new PrintStream("Rules.csv");
		ResultSet rs = DBConnection.fetchData();
		Counter c = new Counter();
		while (rs.next()){
			String name = rs.getString("NAME");
			String lib_to = rs.getString("MIGRATION_LIB_TO");
			if(lib_to!=null) {
				c.incrScore(name, lib_to);
			}
		}
		int unkw = 0;
		int correct = 0;
		for(Pair<String,String> rule : c.getScores().keySet()) {
			String r = rule.getElement0()+":"+rule.getElement1();
			ps.print(rule.getElement0()+";"+rule.getElement1()+";"+c.getScores().get(rule)+";");
			if(rules.contains(r)) {
				ps.println("true");
				correct++;
			}
			else if(wrong_rules.contains(r))
				ps.println("false");
			else{
				unkw++;
				ps.println("u");
			}
		}
		System.out.println(correct+" correct rules found");
		System.out.println(unkw+" unknown rules");
		ps.close();
	}

}
